//
//  BBInviteViewController.m
//  BlockBrowser
//
//  Created by Mac on 2018/4/9.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "BBInviteViewController.h"
#import "BBInviteTableViewCell.h"
#import "TMTabbarViewController.h"
@interface BBInviteViewController ()
@property(nonatomic, strong) STLabel                     *inviteValueLable;
@property(nonatomic, strong) STLabel                     *beginLable;
@property(nonatomic, strong) NSMutableArray                     *dataSouce;
@end

@implementation BBInviteViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"邀请好友拿佣金";
    self.view.backgroundColor = [BBThemeColorManger manger].themeColor;
    self.tableView.backgroundColor = [BBThemeColorManger manger].themeColor;
    [self configSubView];
    [self configFooterView];
    [self sendInvitePeopleHistoryRequest];
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 20)];
    CGFloat buttonWith = (UIScreenWidth - 30)/2;
    STButton * allButton = [[STButton alloc] initWithFrame:CGRectMake(15, 20, buttonWith, 60)
                                                     title:@"全球首款"
                                                titleColor:[UIColor whiteColor]
                                                 titleFont:35
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:nil];
    allButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [header addSubview:allButton];
    
    STButton * keButton = [[STButton alloc] initWithFrame:CGRectMake(allButton.right, 20, buttonWith, 60)
                                                     title:@"可挖矿的"
                                                titleColor:TM_ThemeBackGroundColor
                                                 titleFont:35
                                              cornerRadius:10
                                           backgroundColor:[UIColor whiteColor]
                                           backgroundImage:nil
                                                     image:nil];
    keButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [header addSubview:keButton];
    //@"    "
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(15,keButton.bottom  +10 , UIScreenWidth - 30, 30)
                                                     text:@"区    块    链    媒    体    平    台"
                                                textColor:[UIColor whiteColor]
                                                     font:22
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    [header addSubview:titleLable];
    
    UIView * yellowView = [[UIView alloc] initWithFrame:CGRectMake(15, titleLable.bottom + 80, UIScreenWidth - 30, 150)];
    yellowView.layer.cornerRadius = 10;
    yellowView.clipsToBounds = YES;
    yellowView.backgroundColor = [UIColor colorWithGradientStyle:UIGradientStyleTopToBottom withFrame:yellowView.bounds andColors:@[UIColorFromRGBA(0xFA595E),UIColorFromRGBA(0xFFB81F)]];
    [header addSubview:yellowView];
    
    UIImageView * bannerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, titleLable.bottom + 0, UIScreenWidth , 260)];
    bannerImageView.userInteractionEnabled = NO;
    bannerImageView.image = [UIImage imageNamed:@"渐变"];
    bannerImageView.backgroundColor = [UIColor clearColor];
    [header addSubview:bannerImageView];
    __weak typeof(self) weakSelf =  self;
    
    STButton * inviteButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 189, 44)
                                                     title:@"立即邀请"
                                                titleColor:[UIColor whiteColor]
                                                 titleFont:23
                                              cornerRadius:22
                                           backgroundColor:UIColorFromRGBA(0xFA595E)
                                           backgroundImage:nil
                                                     image:nil];
    inviteButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    inviteButton.centerY = yellowView.bottom;
    inviteButton.centerX = UIScreenWidth / 2;
    [inviteButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedInvitePoeple];
    }];
    [header addSubview:inviteButton];
    
    UIImageView * inviteDesImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 30, yellowView.width- 30 , 36)];
    inviteDesImageView.userInteractionEnabled = NO;
    inviteDesImageView.image = [UIImage imageNamed:@"邀请好友来挖矿"];
    inviteDesImageView.backgroundColor = [UIColor clearColor];
    [yellowView addSubview:inviteDesImageView];
    
    STLabel * yellowinviteLable = [[STLabel alloc] initWithFrame:CGRectMake(0, inviteDesImageView.bottom + 10, yellowView.width, 40)
                                                      text:@"一次性奖励+分成收益"
                                                 textColor:[UIColor whiteColor]
                                                      font:22
                                               isSizetoFit:NO
                                             textAlignment:NSTextAlignmentCenter];
    [yellowView addSubview:yellowinviteLable];
    
    
    BBUserModel * user = [STUserManger defult].loginedUser;
    
    STLabel * inviteLable = [[STLabel alloc] initWithFrame:CGRectMake(15, bannerImageView.bottom + 20, UIScreenWidth - 30 - 60, 40)
                                                      text:[NSString stringWithFormat:@"我的邀请链接:%@",
                                                            user.share]
                                                textColor:[UIColor whiteColor]
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [header addSubview:inviteLable];
    
    STButton * copyButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)
                                                     title:@"复制链接"
                                                titleColor:FlatOrange
                                                 titleFont:14
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:nil];
    copyButton.titleLabel.numberOfLines = 0;
    copyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    copyButton.centerY = inviteLable.centerY;
    copyButton.right = UIScreenWidth - 15;
    [copyButton setClicAction:^(UIButton *sender) {
        UIPasteboard * past = [UIPasteboard generalPasteboard];
        [past setString:user.share];
        [SVProgressHUD showSuccessWithStatus:@"复制成功"];
    }];
    [header addSubview:copyButton];
    
    STLabel * beginLable = [[STLabel alloc] initWithFrame:CGRectMake(15, inviteLable.bottom + 10, UIScreenWidth - 30 - 60, 40)
                                                      text:[NSString stringWithFormat:@"开始时间:%@",
                                                            @"2018-3-26 16:43:00"]
                                                 textColor:[UIColor whiteColor]
                                                      font:14
                                               isSizetoFit:NO
                                             textAlignment:NSTextAlignmentLeft];
    [header addSubview:beginLable];
    self.beginLable = beginLable;
    
    
    //
    STLabel * inviteRecordLable = [[STLabel alloc] initWithFrame:CGRectMake(15, beginLable.bottom + 20, 200, 40)
                                                     text:@"邀请奖励"
                                                textColor:[UIColor whiteColor]
                                                     font:20
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [header addSubview:inviteRecordLable];
    
    
 #pragma mark --network value
    STLabel * inviteValueLable = [[STLabel alloc] initWithFrame:CGRectMake(15, beginLable.bottom + 20, 200, 40)
                                                           text:[NSString stringWithFormat:@"累计获得:%@糖果",[BBSystemManger manger].candyName]
                                                       textColor:[UIColor whiteColor]
                                                            font:14
                                                     isSizetoFit:NO
                                                   textAlignment:NSTextAlignmentRight];
    inviteValueLable.right = UIScreenWidth - 15;
    self.inviteValueLable = inviteValueLable;
    [header addSubview:inviteValueLable];
    
    STLabel * lable1 = [self defultLableWithTop:inviteValueLable.bottom title:@"邀请层级"];
    lable1.left = 0;
    [header addSubview:lable1];
    
    STLabel * lable2 = [self defultLableWithTop:inviteValueLable.bottom title:@"一次性奖励"];
    lable2.left = lable1.right;
    [header addSubview:lable2];
    
    STLabel * lable3 = [self defultLableWithTop:inviteValueLable.bottom title:@"后续收益"];
    lable3.left = lable2.right;
    [header addSubview:lable3];
    STLabel * lable4 = [self defultLableWithTop:inviteValueLable.bottom title:@"邀请人数"];
    lable4.left = lable3.right;
    [header addSubview:lable4];
    
    header.height = lable4.bottom + 30;
    self.tableView.tableHeaderView = header;
    
}
- (STLabel*)defultLableWithTop:(CGFloat)top title:(NSString*)title{
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, top, UIScreenWidth / 4, 20)
                                                     text:title
                                                textColor:[UIColor whiteColor]
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    return titleLable;
}
- (void)configFooterView{
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 20)];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(15, 0, 200, 40)
                                                     text:@"邀请规则"
                                                textColor:[UIColor whiteColor]
                                                     font:20
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [footer addSubview:titleLable];
    
    STLabel * valueLable = [[STLabel alloc] initWithFrame:CGRectMake(15, titleLable.bottom  +10, UIScreenWidth - 30, 40)
                                                     text:@""
                                                textColor:[UIColor whiteColor]
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    NSArray * array = [BBSystemManger manger].originDic[@"invite_rule"];
    NSString * finshTitle = @"";
    for (NSInteger i = 1; i < array.count + 1; i ++) {
        NSString * title = array[i - 1];
        finshTitle = [NSString stringWithFormat:@"%@%ld,%@\n",finshTitle,i,title];
    }
    valueLable.text = finshTitle;
    [NSString lableAutoAdjustheightWithLabel:valueLable];
    [footer addSubview:valueLable];
    footer.height = valueLable.bottom + 20;
    self.tableView.tableFooterView = footer;
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    BBInviteTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[BBInviteTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    NSDictionary * dic = self.dataSouce[indexPath.row];
    cell.lable1.text = [dic[@"name"] description];
    cell.lable2.text = [dic[@"award_once"] description];
    cell.lable3.text = [dic[@"award_percent"] description];
    cell.lable4.text = [dic[@"count"] description];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark --Action Method
- (void)onSelctedInvitePoeple{
    TMTabbarViewController * vc = (id)[UIApplication sharedApplication].keyWindow.rootViewController;
    [vc onSelctedPresentMenuWithTitle:@"分享"];
}
#pragma mark --NetWork Method
- (void)sendInvitePeopleHistoryRequest{
    
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",severHeader,@"/api/user/getInviteInfo"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypeGet
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 NSDictionary * resDIC = responseObject;
                                                 NSLog(@"json=%@",resDIC.mj_JSONString);
                                                 NSArray * array = responseObject[@"invite_history"];
                                                 self.dataSouce = [array mutableCopy];
                                                 
                                                 self.inviteValueLable.text = [NSString stringWithFormat:@"累计获得: %@ %@糖果",
                                                                               resDIC[@"coin"],[BBSystemManger manger].candyName];
                                                 
                                                 self.beginLable.text = [NSString stringWithFormat:@"开始时间:%@",
                                                                         resDIC[@"time"]];
                                                 [self.tableView reloadData];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 
                                                 [SVProgressHUD dismiss];
                                                 [self sendInvitePeopleHistoryRequest];
                                             }];
}
@end
